import jmcvideo.*;

String movName = "movie.wmv";
JMCMovie mov;
PImage result;
float[][] kernel = {
{1,1,1,1,1},
{1,1,1,1,1},
{1,1,1,1,1},
{1,1,1,1,1},
{1,1,1,1,1}
};

boolean initialized = false;
 PVector spot;
void setup(){
  size(800, 600,P2D);
  background(0);
  frame.setResizable(true);
  mov = new JMCMovie(this, movName);
  mov.loop();
stroke(color(0,255,0));
      noFill();

  //spot = new PVector(20,20);
}


// Called every time a new frame is available to read
void videoFrameUpdated(JMCMovie m) {
  m.read();
  initialized = true;
}


PVector findSpot(PImage img, float[][] mx, int mx_w, int mx_h){
  float aux;
  int x_coord;
  int y_coord;
  PVector spot = new PVector(0,0);
  float spotVal = 0;
 
  img.loadPixels();
  if(img.pixels.length == 0)
    return new PVector(0,0);
    
  for(int x = 0; x < img.width; x++){
      for(int y = 0; y < img.height; y++){
          aux = 0;
          for(int i = 0; i < mx_w; i++){
              for(int j = 0; j < mx_h; j++){
                //println("(" +i+ ";" +j+ ")");
                x_coord = (x + (i - mx_w/2 + 1) + img.width) % img.width;
                y_coord = (y + (j - mx_h/2 + 1) + img.height) % img.height;

//                 println("(" + x_coord + ";" + y_coord+ ")");
//                aux += blue(img.pixels[x_coord + y_coord*img.height]) * mx[i][j];
                aux += red(img.get(x_coord, y_coord)) * mx[i][j];
                aux += green(img.get(x_coord, y_coord)) * mx[i][j];
                aux += blue(img.get(x_coord, y_coord)) * mx[i][j];
              } // j
          } // i
        //  println(spotVal +"-" +aux + " - (" +x+ ";" +y+ ")");

          if(aux > spotVal){
            spot = new PVector(x,y);
            spotVal = aux;
//            println("JA!");
//               println("(" +x+ ";" +y+ ")");
//               ellipse(x,y,10,10);
          } // if nuevo candidato
         
      } // y
  } // x
 
  return spot; 
} // findSpot


float millisold = 0;

void draw(){  
      image(mov, 0, 0);    
      spot = findSpot(mov, kernel, 3, 3);
      ellipse(spot.x, spot.y, 25, 25);
      ellipse(spot.x, spot.y, 30, 30);
      ellipse(spot.x, spot.y, 27, 27);
}

